import { getDebitTime, getTodayStartWithEnd } from '@/common/getDate'
import { dateMap } from '@/common/date'
import { encode } from '@/utils/utils'
import { mapState, mapGetters } from 'vuex'
import { getTreeProject } from '@/common/datas'

const market = {
  data() {
    return {
      clinicArray: JSON.parse(localStorage.getItem('ClinicListArray')),
      channelOptions: [],
      // datePickerKey: '今日',
      clinicOptions: JSON.parse(localStorage.getItem('clinicLists'))[0]
        .children,
      btnList: [
        { name: '今日' },
        { name: '昨日' },
        { name: '本周' },
        { name: '本月' },
        { name: '同比' },
      ],
      todayBtnList: [{ name: '待回访' }, { name: '已回访' }],
      getDateList: [{ name: '自然时间' }, { name: '扎帐时间' }],
      isChange: '',
      isNormal: '',
      showQuery: false,
      currentId: null,
      radio: '',
      ids: [],
      userType: '',
      clientHeight: '',
      pageSizes: [50, 100, 150, 200],
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', dateMap['今日'])
            },
          },
          {
            text: '昨天',
            onClick(picker) {
              picker.$emit('pick', dateMap['昨日'])
            },
          },
          {
            text: '本周',
            onClick(picker) {
              picker.$emit('pick', dateMap['本周'])
            },
          },
          {
            text: '本月',
            onClick(picker) {
              picker.$emit('pick', dateMap['本月'])
            },
          },
          {
            text: '同比',
            onClick(picker) {
              picker.$emit('pick', dateMap['同比'])
            },
          },
        ],
      },
      hackReset: true,
      defaultTime: ['00:00:00', '23:59:59'],
    }
  },
  created() {
    this.isChange = 0
  },
  watch: {
    datePickerKey: {
      handler() {
        this.handleDateButtonKeyChange()
      },
      immediate: true,
    },
    showQuery: {
      handler(val) {
        if (val) {
          this.clientHeight = document.body.clientHeight - 340
        } else {
          this.clientHeight = document.body.clientHeight - 223
        }
      },
      immediate: true,
    },
  },
  // mounted() {
  //   this.queryParams = { ...QUERY_PARAMS }
  //   this.form = { ...FROM_DATA }
  //   this.getList()
  // },
  computed: {
    ...mapGetters('dict', [
      'sexOptions',
      'phoneOwnershipOptions',
      'marketStatusOptions',
      'marketEntityOptions',
      'professionalOptions',
      'marketMakeProject',
      'billingStatusOptions',
      'spendingHabitsOptions',
      'economicAbilityOptions',
      'yesOrNo',
      'haveChildOptions',
      'maritalStatusOptions',
      'characterOptions',
      'callBackStateOptions',
      'reachStatusOptions',
    ]),
    ...mapState({
      userName: (state) => state.user.name,
    }),
    ...mapGetters('market', ['marketPostList', 'marketChannelList']),
    projectOptions: () => getTreeProject(),
  },
  methods: {
    filtersFormatter(obj, type) {
      if (type == 'varchar') {
        return obj.map(({ dictLabel }) => ({
          text: dictLabel,
          label: dictLabel,
          value: dictLabel,
        }))
      } else {
        return obj.map(({ dictLabel, dictValue }) => ({
          text: dictLabel,
          label: dictLabel,
          value: dictValue,
        }))
      }
    },
    showDatePicker() {
      this.hackReset = false
      this.$nextTick(() => {
        this.hackReset = true
      })
    },
    showHide(e) {
      this.showQuery = e
    },
    handleTodayReturnChange(type, index) {
      console.log(type)
      this.isChange = index
    },
    getToday() {
      //自然时间
      this.datePickerValue = getTodayStartWithEnd()
    },
    getDebit() {
      //扎帐时间
      this.datePickerValueMinor = getDebitTime()
    },
    filterClinic(row) {
      return this.filterClinicFn(row.reservationClinic)
    },
    filterVisitClinic(row) {
      if (row.yiyaMarketReservation == undefined) {
        return this.filterClinicFn(row.reservationClinic)
      } else {
        return this.filterClinicFn(row.yiyaMarketReservation.reservationClinic)
      }
    },
    filterClinicFn(clinicId) {
      if (this.clinicArray.length == 0) {
        return
      }
      const item = this.clinicArray.filter((item) => {
        if (clinicId == item.deptId) {
          return item
        }
      })
      if (item[0] == undefined) {
        return ''
      } else {
        return item[0].deptName
      }
    },
    //点击日期按钮高亮和切换时间
    handleDateButtonKeyChange() {
      this.datePickerValue = getTodayStartWithEnd()
      // if (this.$route.path == '/market/reservationArrived') {
      //   this.isNormal = 0
      //   this.datePickerValueMinor = getTodayStartWithEnd()
      // }
      this.changeTime(this.datePickerValue)
    },
    //筛选字典
    formatterDict(value, optionsKey) {
      return this.selectDictLabel(this[optionsKey], value)
    },
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex
    },
    showBgc({ row, column, rowIndex, columnIndex }) {
      if (this.selectedIndex === rowIndex) {
        return { 'background-color': '#b1daff !important' }
      }
    },
    //点击row跳转和单选
    handleDetails(row, column, event) {
      if (
        event.target._prevClass == 'el-radio__inner' ||
        event.target._prevClass == 'el-radio__label' ||
        event.target._prevClass == 'el-radio__input' ||
        event.target._prevClass == 'el-radio__original'
      ) {
        event.preventDefault()
        this.radio = this.list.indexOf(row)
        this.ids = [row]
        this.multiple = false
        this.selectedIndex = row.index
        this.userType = row.channelFirst
        return
      }
      if (event.target._prevClass == undefined) {
        if (
          row.yiyaMarketReservation !== undefined &&
          row.yiyaMarketReservation.id
        ) {
          this.goUserMessageHandle(row.yiyaMarketReservation.id, '')
        } else {
          this.goUserMessageHandle(row.id, '')
        }
      }
    },
    doubleHandle(row) {
      if (
        row.yiyaMarketReservation.id !== undefined &&
        row.yiyaMarketReservation.id
      ) {
        this.goUserMessageHandle(row.yiyaMarketReservation.id, '')
      } else {
        this.goUserMessageHandle(row.id, '')
      }
    },
    goUserMessageHandle(id, marketId) {
      this.$router.push(
        `/userMessageHandle/userHandle?id=${id}&marketId=${marketId}&redirect=${encode(
          {
            path: this.$route.path,
            query: this.queryParams,
          }
        )}`
      )
    },
    handleExport(fn) {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return fn(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function () {})
    },
  },
}
export default market
